/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.preferences.formatter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.dltk.internal.ui.IDLTKStatusConstants;
import org.eclipse.php.internal.ui.PHPUiPluginExt;
import org.eclipse.php.internal.ui.corext.util.CodeFormatterUtil;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.text.edits.TextEdit;

/**
 * 
 * @author Sean W. Quinn (swquinn@gmail.com)
 */
@SuppressWarnings("restriction")
public class SnippetPreview extends PHPPreview {

    public final static class PreviewSnippet {

        public String header;
        public final String source;
        public final int kind;

        public PreviewSnippet(int kind, String source) {
            this.kind= kind;
            this.source= source;
        }
    }

    private ArrayList fSnippets;

    public SnippetPreview(Map workingValues, Composite parent) {
        super(workingValues, parent);
        fSnippets= new ArrayList();
    }

    @Override
	protected void doFormatPreview() {
        if (fSnippets.isEmpty()) {
            fPreviewDocument.set(""); //$NON-NLS-1$
            return;
        }

        //This delimiter looks best for invisible characters
        final String delimiter= "\n"; //$NON-NLS-1$

        final StringBuffer buffer= new StringBuffer();
        for (final Iterator iter= fSnippets.iterator(); iter.hasNext();) {
            final PreviewSnippet snippet= (PreviewSnippet) iter.next();
            String formattedSource;
            try {
            	TextEdit textEdit = CodeFormatterUtil.format(snippet.kind, snippet.source, 0, delimiter, fWorkingValues);
                formattedSource = textEdit.toString();
            } catch (Exception e) {
                final IStatus status= new Status(IStatus.ERROR, PHPUiPluginExt.getPluginId(), IDLTKStatusConstants.INTERNAL_ERROR,
                    PHPFormatterMessages.PHPPreview_formatter_exception, e);
                PHPUiPluginExt.log(status);
                continue;
            }
            buffer.append(delimiter);
            buffer.append(formattedSource);
            buffer.append(delimiter);
            buffer.append(delimiter);
        }
        fPreviewDocument.set(buffer.toString());
    }



    public void add(PreviewSnippet snippet) {
        fSnippets.add(snippet);
    }

    public void remove(PreviewSnippet snippet) {
        fSnippets.remove(snippet);
    }

    public void addAll(Collection snippets) {
        fSnippets.addAll(snippets);
    }

    public void clear() {
        fSnippets.clear();
    }

}
